package com.ruoyi.backend.service.impl;

import com.ruoyi.backend.mapper.UserManageMapper;
import com.ruoyi.backend.service.IUserAppService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserAppServiceImpl implements IUserAppService {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private UserManageMapper userManageMapper;

    /**
     * 生成token信息
     *
     * @param loginUser
     * @return
     */
    @Override
    public String createToken(LoginUser loginUser) {
        return tokenService.createToken(loginUser);
    }


    /**
     * 查询APP端用户信息
     *
     * @param userId APP端用户信息主键
     * @return APP端用户信息
     */
    @Override
    public SysUser selectUserByUserId(Long userId) {
        return userMapper.selectUserById(userId);
    }

    /**
     * 查询APP端用户信息列表
     *
     * @param sysUser APP端用户信息
     * @return APP端用户信息
     */
    @Override
    public List<SysUser> selectUserList(SysUser sysUser) {
        return userMapper.selectUserList(sysUser);
    }

    /**
     * 新增APP端用户信息
     *
     * @param sysUser APP端用户信息
     * @return 结果
     */
    @Override
    public int insertAppUser(SysUser sysUser) {
        if (null == sysUser.getUserId()) {
            throw new ServiceException("用户ID不能为空");
        }
        if (StringUtils.isEmpty(sysUser.getUserName())) {
            throw new ServiceException("用户账号不能为空");
        }
        if (StringUtils.isEmpty(sysUser.getPhonenumber())) {
            throw new ServiceException("用户手机号码不能为空");
        }
        //新增账号设置为正常未删除状态
        sysUser.setStatus("0");
        sysUser.setDelFlag("0");
        sysUser.setCreateTime(new Date());

        //校验用户是否已经存在
        Integer num = userManageMapper.countUserByUsername(sysUser);
        if (null != num && num > 0) {
            throw new ServiceException("用户已经存在,请检查用户名！");
        }
        return userMapper.insertUser(sysUser);
    }

    /**
     * 修改APP端用户信息
     *
     * @param sysUser APP端用户信息
     * @return 结果
     */
    @Override
    public int updateAppUser(SysUser sysUser) {
        if (null == sysUser.getUserId()) {
            throw new ServiceException("用户ID不能为空");
        }
//        if(StringUtils.isEmpty(gspUser.getUserName())){
//            throw new ServiceException("用户账号不能为空");
//        }
//        if(StringUtils.isEmpty(gspUser.getPhonenumber())){
//            throw new ServiceException("用户手机号码不能为空");
//        }
//        if(StringUtils.isEmpty(gspUser.getUserType())){
//            throw new ServiceException("用户类型不能为空");
//        }
        SysUser queryUser = userMapper.selectUserById(sysUser.getUserId());
        if (null == queryUser) {
            throw new ServiceException("用户不存在");
        }

        return userMapper.updateUser(sysUser);
    }

    /**
     * 记录登录信息
     *
     * @param userId 用户ID
     */
    @Override
    public void recordLoginInfo(Long userId) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setLoginIp(IpUtils.getIpAddr());
        sysUser.setLoginDate(DateUtils.getNowDate());
        userMapper.updateUser(sysUser);
    }

}
